package org.hnxxxy.ebms.common;

import org.springframework.stereotype.Component;

/**
 * @Author: wei
 * @Date: 2020/7/13 9:14
 */
@Component
public class Result<T> {

    private int code; // 200 is success, not 200 is exception
    private String msg;
    private T data;

    public static <T> Result<T> success(int code,String msg,T data){
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(T data){
        return success(200,"操作成功",data);
    }

    public static <T> Result<T> success(int code,String msg){
        return success(code,msg,null);
    }

    public static <T> Result<T> fail(int code,String msg,T data){
        Result<T> result = new Result<T>();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> fail(String msg,T data){
        return fail(400,msg,data);
    }

    public static <T> Result<T> fail(String msg){
        return fail(400,msg,null);
    }

    public static <T> Result<T> fail(int code,String msg){
        return fail(code,msg,null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
